package component;

import java.awt.Button;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import Prism.core.AbstractImplementation;
import Prism.core.Event;
import Prism.core.PrismConstants;

public class TaskProcessorGUI extends AbstractImplementation implements ActionListener {
	// Declare GUI AWT frame
	Frame frame;
	
	// About TOP info GUI
	Panel topPanel;
		
	// About new User GUI
	Panel newUserPanel;
	TextField newUserPhoneTextField;
	TextField newUsernameTextField;
	
	// About Task GUI
	Panel taskPanel;
	Button createButton;
	Button provideUserDataButton;
	Button sendButton;
	Button providePhoneButton;
	Button showTimeButton;
	
	// About Administrative GUI
	Panel adminPanel;
	TextField timeTextField;
	Button closeButton;
	
	@Override
	public void start() {
		super.start();
		
		// initialize GUI
		initGUI();
	}
	
	@Override
	public void handle(Event e) {
		if (e.getName().equals(CustomerSupport.provCreateNewUser)) {
			newUsernameTextField.setText((String) e.getParameter("name"));
			newUserPhoneTextField.setText((String) e.getParameter("phoneNumber"));
		}
		else if (e.getName().equals(CustomerSupport.provProvideUserData)) {
			newUsernameTextField.setText((String) e.getParameter("name"));
			newUserPhoneTextField.setText((String) e.getParameter("phoneNumber"));
		}
		else if (e.getName().equals(CustomerSupport.provProvidePhoneNumber)) {
			newUserPhoneTextField.setText((String) e.getParameter("phoneNumber"));
		}
		else if (e.getName().equals(CustomerSupport.provShowTime)) {
			timeTextField.setText((String) e.getParameter("time"));
		}
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// Action handling of Close button, close Billing System UI
		if (e.getSource() == closeButton) {
			System.out.println("Closing...");
			frame.setVisible(false); // hide the Frame
			frame.dispose(); // free the system resources
			System.exit(0); // close the application
		}
		// Action handling of createButton
		else if (e.getSource() == createButton) {
			Prism.core.Event r = new Prism.core.Event(CustomerSupport.provCreateNewUser, PrismConstants.REQUEST);
			this.send(r);
		}
		// Action handling of provideUserDataButton
		else if (e.getSource() == provideUserDataButton) {
			Prism.core.Event r = new Prism.core.Event(CustomerSupport.provProvideUserData, PrismConstants.REQUEST);
			this.send(r);
		}
		// Action handling of sendButton
		else if (e.getSource() == sendButton) {
			//r.addParameter("UserRecordType", phoneInput.getText().trim());
			//r.addParameter("Operation", phoneInput.getText().trim());
			Prism.core.Event r = new Prism.core.Event(CustomerSupport.provSendUserDataOperation, PrismConstants.REQUEST);
			this.send(r);
		}
		// Action handling of providePhoneButton
		else if (e.getSource() == providePhoneButton) {
			Prism.core.Event r = new Prism.core.Event(CustomerSupport.provProvidePhoneNumber, PrismConstants.REQUEST);
			r.addParameter("taskID", String.valueOf(Math.random()*10000));
			this.send(r);
		}
		// Action handling of showTimeButton
		else if (e.getSource() == showTimeButton) {
			Prism.core.Event r = new Prism.core.Event(CustomerSupport.provShowTime, PrismConstants.REQUEST);
			r.addParameter("taskID", String.valueOf(Math.random()*10000));
			this.send(r);
		}
	}
	 
	// this function creates GUI
	// this is private because creating GUI is inside job of this instance
	// In other words, this is initializing, other instances is free to initialize
	private void initGUI() {
		frame = new Frame("Task Processor GUI");
		
		// Top Info GUI
		topPanel = new Panel();
		topPanel.setLayout(new GridLayout(3,1));
		topPanel.add(new Label("Task Processor GUI"));
		topPanel.add(new Label("1779-1469-26"));
		topPanel.add(new Label("Kwan Park"));

		
		// About new User GUI
		newUserPanel = new Panel();
		newUsernameTextField = new TextField();
		newUsernameTextField.setEditable(true);
		newUserPhoneTextField = new TextField();
		newUserPhoneTextField.setEditable(true);
		newUserPanel.setLayout(new GridLayout(4,1));
		newUserPanel.add(new Label("New User(Below)"));
		newUserPanel.add(newUsernameTextField);
		newUserPanel.add(new Label("New Phone#(Below)"));
		newUserPanel.add(newUserPhoneTextField);
		
		// About Task GUI
		taskPanel = new Panel();
		createButton = new Button("Create New User");
		provideUserDataButton = new Button("Provide User Data");
		sendButton = new Button("Send User Data & Operation");
		providePhoneButton = new Button("Provide Phone #");
		showTimeButton = new Button("Show Time");
		taskPanel.setLayout(new GridLayout(5,1));
		taskPanel.add(createButton);
		taskPanel.add(provideUserDataButton);
		taskPanel.add(sendButton);
		taskPanel.add(providePhoneButton);
		taskPanel.add(showTimeButton);
					
		// Administrative GUI
		adminPanel = new Panel();
		adminPanel.setLayout(new GridLayout(2,1));
		closeButton = new Button("close");
		timeTextField = new TextField();
		timeTextField.setEditable(false);
		adminPanel.add(timeTextField);
		adminPanel.add(closeButton);
		
		
		// Register Listener
		createButton.addActionListener(this);
		provideUserDataButton.addActionListener(this);
		sendButton.addActionListener(this);
		providePhoneButton.addActionListener(this);
		showTimeButton.addActionListener(this);
		closeButton.addActionListener(this);
		
		frame.setLayout(new GridLayout(4,1));
		frame.add(topPanel);
		frame.add(newUserPanel);
		frame.add(taskPanel);
		frame.add(adminPanel);
		frame.setVisible(true);
		frame.pack();		
	}
}
